package com.iwdnb.gkgz.common.quota;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.google.common.collect.Lists;
import com.iwdnb.gkgz.common.utils.BigDecimalUtils;
import lombok.Data;

public class KDJIndicator2 {

    private static final int N = 14; // KDJ计算周期
    private static final int M1 = 3;  // K值平滑周期
    private static final int M2 = 3;  // D值平滑周期

    public static void main(String[] args) {
        // 示例数据：假设为一段时间内的收盘价
        List<Double> closePrices = Lists.newArrayList(23.2, 24.5, 24.1, 25.0,
            26.2, 25.5, 27.8, 28.5,
            27.2, 29.0, 30.1, 31.5,
            32.0, 32.5, 33.0);
        List<KDJResult> kdjResults = calculateKDJ(closePrices);

        // 打印结果
        for (int i = 0; i < kdjResults.size(); i++) {
            KDJResult result = kdjResults.get(i);
            System.out.printf("Day %d: K=%.2f, D=%.2f, J=%.2f\n",
                i + 1, result.getK(), result.getD(), result.getJ());
        }
    }

    public static List<KDJResult> calculateKDJ(List<Double> closePrices) {
        List<KDJResult> results = new ArrayList<>();
        List<Double> RSV = new ArrayList<>();

        for (int i = 0; i < closePrices.size(); i++) {
            if (i < N - 1) {
                results.add(new KDJResult("", 0, 0, 0)); // 初始值
                continue;
            }

            double highestHigh = Double.MIN_VALUE;
            double lowestLow = Double.MAX_VALUE;

            // 计算N日内的最高价和最低价
            for (int j = 0; j < N; j++) {
                double price = closePrices.get(i - j);
                if (price > highestHigh) {highestHigh = price;}
                if (price < lowestLow) {lowestLow = price;}
            }

            // 计算RSV
            double currentClose = closePrices.get(i);
            double rsv = ((currentClose - lowestLow) / (highestHigh - lowestLow)) * 100;
            RSV.add(rsv);
        }

        double K = 50, D = 50;

        for (int i = 0; i < RSV.size(); i++) {
            if (i < M1) {
                //results.get(i).setK(K);
                //results.get(i).setD(D);
                //results.get(i).setJ(3 * K - 2 * D); // 初始J值的计算
                continue;
            }

            // K值计算
            K = (1.0 / M1) * RSV.get(i) + (1 - (1.0 / M1)) * K;
            // D值计算
            D = (1.0 / M2) * K + (1 - (1.0 / M2)) * D;
            // J值计算
            double J = 3 * K - 2 * D;

            //results.get(i).setK(K);
            //results.get(i).setD(D);
            //results.get(i).setJ(J);
        }

        return results;
    }

    @Data
    public static class KDJResult {
        private String datetime;
        private BigDecimal K;
        private BigDecimal D;
        private BigDecimal J;

        public KDJResult(String datetime, double k, double d, double j) {
            this.datetime = datetime;
            K = BigDecimalUtils.of(k);
            D = BigDecimalUtils.of(d);
            J = BigDecimalUtils.of(j);
        }


    }
}

